package com.me.services;

import com.me.utils.IOUtility;

import java.lang.reflect.InvocationTargetException;

public class ServiceManager {
	private static Class userServEntity;
	private static Class itemServEntity;
//	private static Class userEntity;
//	private static Class itemEntity;
	
	
	static {
		try {
			String servPkg = IOUtility.getProperty("data.service");
//			String entiPkg = IOUtility.getProperty("data.entity");
			
			int servStartIndex = servPkg.lastIndexOf(".") + 1;
//			int entiStartIndex = entiPkg.lastIndexOf(".") + 1;
			
			String servSuffix = servPkg.substring(servStartIndex, servStartIndex + 1).toUpperCase() + servPkg.substring(servStartIndex + 1) + "Service";
//			String entiPrefix = entiPkg.substring(entiStartIndex, entiStartIndex + 1).toUpperCase() + entiPkg.substring(entiStartIndex + 1);
			
			userServEntity = Class.forName(servPkg + ".User" + servSuffix);
			itemServEntity = Class.forName(servPkg + ".Item" + servSuffix);
//			userEntity = Class.forName(entiPkg + "." + entiPrefix + "User");
//			itemEntity = Class.forName(entiPkg + "." + entiPrefix + "Item");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	public static IItemService getItemService() {
		try {
			return (IItemService) itemServEntity.getDeclaredConstructor().newInstance();
		} catch (IllegalAccessException | InstantiationException | NoSuchMethodException | InvocationTargetException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static IUserService getUserService() {
		try {
			return (IUserService) userServEntity.getDeclaredConstructor().newInstance();
		} catch (IllegalAccessException | InstantiationException | NoSuchMethodException | InvocationTargetException e) {
			e.printStackTrace();
		}
		return null;
	}
}
